﻿using System.Linq;
using System.ServiceModel.DomainServices.Client;
using GalaSoft.MvvmLight;
using PhotoManagerApp.Web.DomainServices;
using PhotoManagerApp.Web.EF;

namespace PhotoManagerApp.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private EroticDomainContext context = new EroticDomainContext();

        public MainViewModel()
        {
            RefreshUsers();
        }

        public EntitySet<Image> Images
        {
            get
            {
                return context.Images;
            }
        }

        public EntitySet<aspnet_Users> Users
        {
            get
            {
                return context.aspnet_Users;
            }
        }

        public EntitySet<Folder> Folders
        {
            get
            {
                return context.Folders;
            }
        }

        private aspnet_Users currentUser;
        public aspnet_Users CurrentUser
        {
            get
            {
                return currentUser;
            }
            set
            {
                if (this.currentUser != value)
                {
                    currentUser = value;
                    RefreshFolders();
                    RaisePropertyChanged("CurrentUser");
                }
            }
        }

        private aspnet_Users loggedInUser;
        public aspnet_Users LoggedInUser
        {
            get
            {
                return loggedInUser;
            }
            set
            {
                if (this.loggedInUser != value)
                {
                    loggedInUser = value;
                    RaisePropertyChanged("LoggedInUser");
                }
            }
        }

        private Folder currentFolder;
        public Folder CurrentFolder
        {
            get
            {
                return this.currentFolder;
            }
            set
            {
                if (this.currentFolder != value)
                {
                    this.currentFolder = value;
                    LoadImages();
                    RaisePropertyChanged("CurrentFolder");
                }
            }
        }

        private Image currentImage;
        public Image CurrentImage
        {
            get
            {
                return currentImage;
            }
            set
            {
                currentImage = value;
                RaisePropertyChanged("CurrentImage");
            }
        }

        private void LoadImages()
        {
            context.Images.Clear();
            var query = context.GetImagesQuery().Where(f => f.FolderID == CurrentFolder.FolderID);
            context.Load(query, lo =>
            {
            }, null);
        }

        public void RefreshFolders()
        {
            context.Folders.Clear();
            var query = context.GetFoldersQuery().Where(f => f.aspnet_Users.UserName == CurrentUser.UserName);
            context.Load(query, lo =>
            {
            }, null);
        }

        public void RefreshUsers()
        {
            context.aspnet_Users.Clear();
            var query = context.GetAspnet_UsersQuery();
            context.Load(query, lo =>
            {
                this.LoggedInUser = Users.FirstOrDefault(u => u.UserName == WebContext.Current.User.Name);
            }, null);
        }

        internal void SaveContext()
        {
            context.SubmitChanges();
        }
    }
}
